import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/custom_picker.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/view_model/my_page_vmodel.dart';

class EditInfo extends StatefulWidget {
  EditInfo({Key key}) : super(key: key);

  @override
  _EditInfo createState() {
    return _EditInfo();
  }
}

class _EditInfo extends State<EditInfo> {
  var li1 = ['昵称', '简介'];
  var li2 = ['姓名', '安小', '姓别', '生日', '婚姻状态', '所在城市'];
  var li3 = [
    '行业',
    '职位',
    '邮箱',
  ];

  var list = {
    "member": {
      "username": {"value": "安妮", "list": null, "edit": 1},
      "chi_name": {"value": "安小", "list": null, "edit": 1},
      "eng_name": {"value": null, "list": null, "edit": 1},
      "mobile": {"value": "13535443779", "list": null, "edit": 1},
      "description": {"value": "热爱学习、正所谓书山有路先干为敬", "list": null, "edit": 1},
      "email": {"value": null, "list": null, "edit": 1},
      "hkid": {"value": null, "list": null, "edit": 1},
      "city": {"value": "深圳", "list": null, "edit": 1},
      "dob": {"value": "2000-06-05", "list": null, "edit": 1},
      "sex": {"value": "F", "list": null, "edit": 1},
      "marital_satus": {"value": 1, "list": null, "edit": 1},
      "company": {"value": null, "list": null, "edit": 1},
      "title": {"value": null, "list": null, "edit": 1},
      "company_nature": {"value": null, "list": null, "edit": 1},
      "occupation": {"value": 2, "list": null, "edit": 1},
      "working_cat": {"value": "16", "list": null, "edit": 1},
      "header_img": {
        "value":
            "https://api.in-monkeys.com/upload_member/base64_image/1572/4355dca77072dfe61674128c125b7ec1.png",
        "list": null,
        "edit": 1
      },
      "team_no": {"value": null, "list": null, "edit": 1},
      "swear_detail": {"value": null, "list": null, "edit": 1},
      "position": {"value": null, "list": null, "edit": 1},
      "cradle_song": {"value": null, "list": null, "edit": 1}
    }
  };
  List<String> list1 = [
    '简体字体',
    '推送通知',
  ];
  String curCity = '';
  int selIndex;
  int groupValue = 0;
  bool isBinding = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  String _name;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('个人资料'),
      ),
      body: ProviderWidget<MyPageMemberDetailVModel>(
        model: MyPageMemberDetailVModel(),
        onModelReady: (model) => model.initData(),
        builder: (context, model, child) {
          if (model.isBusy) print('正在加载中');
          if (model.isEmpty) print('空数据');
          if (model.isError) print('出错了');

          return (model.datas == null)
              ? LoadingWidget()
              : ListView.builder(
                  shrinkWrap: true,
                  itemCount: list.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Column(
                      children: [
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppColor.dividerColor, width: 1))),
                          padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                          height: 55,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '帐号信息',
                                style: TextStyle(fontSize: AppFontSize.Size18),
                              ),
                            ],
                          ),
                        ),
                        Column(
                          children: List.generate(
                              model.acountDatas.length,
                              (index) => _customInfoCell(
                                  title: model.acountDatas[index]['title'],
                                  des: model.acountDatas[index]['val'],
                                  ontap: () {
                                    print('哈哈');
                                    Navigator.pushNamed(
                                        context, RouterName.editInput,
                                        arguments: model.acountDatas[index]);
                                  })),
                        ),
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppColor.dividerColor, width: 1))),
                          padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                          height: 55,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '个人信息',
                                style: TextStyle(fontSize: AppFontSize.Size18),
                              ),
                            ],
                          ),
                        ),
                        Column(
                          children: List.generate(
                              model.userDatas.length,
                              (index) => _customInfoCell(
                                  title: model.userDatas[index]['title'],
                                  des: model.userDatas[index]['val'],
                                  ontap: () {
                                    var datas = model.userDatas[index];
                                    print('object: + ${datas}');
                                    return (datas['type'] == 'input')
                                        ? Navigator.pushNamed(
                                            context, RouterName.editInput,
                                            arguments: model.userDatas[index])
                                        : showPicker(
                                            type: datas['type'],
                                            datas: datas['datas'],
                                            callBack: (var index, var str) {
                                               //如果是性别 则要转回后台需要的字符
                                              if (datas['key'] == 'sex')
                                                (str == '男')
                                                    ? str = 'M'
                                                    : str = 'F';
                                              SmonkeysRepository
                                                  .fetchMyEditorSave(datas: {
                                                datas["key"]: str
                                              });
                                            });
                                  })),
                        ),
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppColor.dividerColor, width: 1))),
                          padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                          height: 55,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '职业信息',
                                style: TextStyle(fontSize: AppFontSize.Size18),
                              ),
                            ],
                          ),
                        ),
                        Column(
                          children: List.generate(
                              model.jobDatas.length,
                              (index) => _customInfoCell(
                                  title: model.jobDatas[index]['title'],
                                  des: model.jobDatas[index]['val'],
                                  ontap: () {
                                    var datas = model.jobDatas[index];
                                    return (datas['type'] == 'input')
                                        ? Navigator.pushNamed(
                                        context, RouterName.editInput,
                                        arguments: datas)
                                        : showPicker(
                                        type: datas['type'],
                                        datas: datas['datas'],
                                        callBack: (var index, var str) {
                                          //如果是性别 则要转回后台需要的字符
                                          SmonkeysRepository
                                              .fetchMyEditorSave(datas: {
                                            datas["key"]: str
                                          });
                                        });
                                  })),
                        ),
                      ],
                    );
                  });
        },
      ),
    );
  }

  // 用户资料cell
  Widget _customInfoCell({String title, String des, Function ontap}) {
    return GestureDetector(
      onTap: ontap,
      child: Container(
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
                bottom: BorderSide(color: AppColor.dividerColor, width: 1))),
        padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
        height: 55,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(title),
            Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(right: 10),
                  child: Offstage(
                    offstage: isBinding,
                    child: Text(des),
                  ),
                ),
                IconFonts.rightIcon,
              ],
            ),
          ],
        ),
      ),
    );
  }

  //根据不同的类型显示不同的选择器
  showPicker({String type, var datas, Function callBack}) {
    if (type == 'singleSelect') {
      return CustomPicker.showStringPicker(context,
          data: datas, clickCallBack: callBack);
    } else {
      return CustomPicker.showDatePicker(context,
          dateType: DateType.YMD,
//            dateType: DateType.YM,
//            dateType: DateType.YMD_HM,
//            dateType: DateType.YMD_AP_HM,
//             title: "请选择2",
//            minValue: DateTime(2020,10,10),
//            maxValue: DateTime(2023,10,10),
            value: DateTime(datas[0],datas[1],datas[2]),
          clickCallback: callBack);
    }
  }
}
